package com.dz.controller.admin;

import com.dz.constant.JwtClaimsConstant;
import com.dz.context.BaseContext;
import com.dz.dto.EmployeeDTO;
import com.dz.dto.EmployeeLoginDTO;
import com.dz.dto.EmployeePageQueryDTO;
import com.dz.dto.PasswordEditDTO;
import com.dz.entity.Employee;
import com.dz.properties.JwtProperties;
import com.dz.result.Result;
import com.dz.service.EmployeeService;
import com.dz.utils.JwtUtil;
import com.dz.vo.EmployeeLoginVO;
import com.dz.vo.EmployeeVo;
import com.dz.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口") // 描述这个类
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;
    @Resource
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO 员工Dto
     */
    @ApiOperation(value = "员工登陆")  // 描述这个方法的作用
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
        return Result.success(employeeLoginVO);
    }
    /**
     * 退出
     */
    @ApiOperation("员工退出")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /*
     *新增员工
     * */
    @ApiOperation("新增员工")
    @PostMapping()
    public Result<?> addEmployee(@RequestBody EmployeeDTO employeeDTO) {
        return employeeService.addEmployee(employeeDTO);
    }


    /*
     * 分页查询员工
     * */
    @ApiOperation("员工列表分页查询")
    @GetMapping("/page")
    public Result<PageVo> getEmployeeList(EmployeePageQueryDTO pageQueryDTO) {
        PageVo page = employeeService.getEmployeePage(pageQueryDTO);
        return Result.success(page);
    }

    /*
    * 启用、禁用员工账号
    * */
    @ApiOperation("启用、禁用员工账号")
    @PostMapping("/status/{status}")
    public Result<String> changeStatus(@PathVariable Integer status, Long id) {
        return employeeService.changeStatus(status,id);
    }

    /*
    * 根据id查询员工
    * */
    @ApiOperation("根据id查询员工")
    @GetMapping("{id}")
    public Result<EmployeeVo> getEmpById(@PathVariable Long id) {
        return employeeService.getEmployeeById(id);
    }

    /*
    * 编辑员工
    * */
    @ApiOperation("编辑员工")
    @PutMapping()
    public Result<String> updateEmp(@RequestBody EmployeeDTO employeeDTO) {
        return employeeService.updateEmp(employeeDTO);
    }


    @ApiOperation("修改密码")
    @PutMapping("/editPassword")
    public Result<String> editPass(@RequestBody PasswordEditDTO passwordEditDTO) {
        passwordEditDTO.setEmpId(BaseContext.getCurrentId());
        return employeeService.editPass(passwordEditDTO);
    }

}
